<!-- 排班设置页 -->
<template>
  <div class="dashboard-container waybill-list customer-list-box">
    <el-card class="top">
      <el-button
        class="btn"
        @click="openDialog"
      >添加工作模式</el-button>
      <el-table
        class="el-tab"
        :data="list"
        style="width: 100%"
      >
        <el-table-column
          label="工作模式编号"
          prop="id"
        > </el-table-column>
        <el-table-column
          label="工作模式"
          prop="name"
        > </el-table-column>
        <el-table-column
          label="类型"
          prop="workPatternTypeDesc"
        >
        </el-table-column>
        <el-table-column
          label="工作日期"
          prop="workDate"
        > </el-table-column>
        <el-table-column label="工作时间">
          <template #default="{ row }">
            <span>{{
              parseInt((row.workEndMinute1 - row.workStartMinute1) / 60) +
                '小时'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              style="margin-left: -135px"
              class="one"
              @click="editList(row)"
            >编辑</el-button>
            <el-button
              type="text"
              class="two"
              @click="deleteWorkHistoryList(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="pag"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      width="50%"
      :title="title"
      :visible="isDialog"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        label-width="110px"
        :model="formData"
        :rules="rules"
      >
        <el-tabs
          v-model="formData.workPatternType"
          @tab-click="handleClick"
        >
          <el-tab-pane

            name="1"
            label="礼拜制"
          ></el-tab-pane>
          <el-tab-pane

            name="2"
            label="连续制"
          ></el-tab-pane>
        </el-tabs>
        <el-form-item
          label="工作模式名称"
          prop="name"
        >
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item
          v-if="formData.workPatternType === '1'"
          label="连续工作天数"
        >
          <el-checkbox v-model="formData.monday">周一</el-checkbox>
          <el-checkbox v-model="formData.tuesday">周二</el-checkbox>
          <el-checkbox v-model="formData.wednesday">周三</el-checkbox>
          <el-checkbox v-model="formData.thursday">周四</el-checkbox>
          <el-checkbox v-model="formData.friday">周五</el-checkbox>
          <el-checkbox v-model="formData.saturday">周六</el-checkbox>
          <el-checkbox v-model="formData.sunday">周日</el-checkbox>
          <div>选中为工作时间，未选中为休息时间</div>
        </el-form-item>
        <el-form-item
          v-else
          label="连续工作天数"
        >
          上<el-input
            v-model="formData.workDayNum"
            class="el-ipt"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >天</i>
          </el-input>
          休<el-input
            v-model="formData.restDayNum"
            class="el-ipt"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >天</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="工作模式名称"
          prop="value1"
        >
          <el-time-picker
            ref="time"
            v-model="value1"
            is-range
            step
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            @change="changeTime"
          >
          </el-time-picker>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="btn"
          type="primary"
          @click="addWorkHistoryList"
        >确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  workHistoryList,
  deleteWorkHistory,
  addWorkHistory,
  workHistoryInfo,
  updateWorkHistory
} from '@/api/transit'
export default {
  data() {
    return {
      list: [],
      page: 1,
      pageSize: 10,
      total: 0,
      isDialog: false,

      formData: {
        id: 0,
        friday: '0',
        monday: '0',
        name: '',
        operator: '',
        restDayNum: 0,
        saturday: '0',
        sunday: '0',
        thursday: '0',
        tuesday: '0',
        userType: 0,
        wednesday: '0',
        workDayNum: '0',
        workPatternType: '1',
        workStartMinute1: '',
        workEndMinute1: ''

      },
      value1: [new Date(2016, 9, 10, 8, 40), new Date(2016, 9, 10, 9, 40)],
      rules: {
        name: [
          { required: true, message: '请输入工作模式名称', trigger: 'blur' }
        ],
        // monday: [
        //   { required: true, message: '*号位必填项', trigger: 'blur' }
        // ],
        // workDayNum: [
        //   { required: true, message: '*号位必填项', trigger: 'blur' }
        // ],
        val: ''
      }
    }
  },
  computed: {
    title() {
      if (this.formData.id) {
        return '编辑工作模式'
      } else {
        return '添加工作模式'
      }
    }
  },

  created() {
    this.workHistory()
  },
  methods: {
    // 时间分钟数格式化
    minTime(min) {
      const h = Math.floor(min / 60)
      min -= h * 60
      return min
    },
    hTime(min) {
      const h = Math.floor(min / 60)
      min -= h * 60
      return h
    },
    TimeFinish(data) {
      const timestar = this.minTime(data.workStartMinute1)
      const timestarh = this.hTime(data.workStartMinute1)
      const timeend = this.minTime(data.workEndMinute1)
      const timeendh = this.hTime(data.workEndMinute1)
      // console.log(timestar)
      this.value1 = [new Date(2023, 6, 6, timestarh, timestar), new Date(2023, 6, 6, timeendh, timeend)]
    },
    handleClick(val) {
      console.log(val)
    },

    /**
     * 编辑
     */
    async editList(row) {
      this.isDialog = true

      const res = await workHistoryInfo(row.id)
      this.TimeFinish(res.data)
      // this.formData.workPatternType = res.data.workPatternType + ''
      // this.formData = res.data
      this.formData.id = res.data.id
      this.formData.name = res.data.name
      this.formData.operator = res.data.operator
      this.formData.restDayNum = res.data.restDayNum
      this.formData.userType = res.data.userType
      this.formData.workDayNum = res.data.workDayNum
      this.formData.workPatternType = res.data.workPatternType + ''
      this.formData.workStartMinute1 = res.data.workStartMinute1
      this.formData.workEndMinute1 = res.data.workEndMinute1
      this.formData.tuesday = res.data.tuesday
      this.formData.wednesday = res.data.wednesday
      this.formData.thursday = res.data.thursday
      this.formData.friday = res.data.friday
      this.formData.saturday = res.data.saturday
      this.formData.sunday = res.data.sunday
      console.log(res.data)
    },
    /**
     * 处理时间
     */
    changeTime(val) {
      this.formData.workStartMinute1 =
        val[0].getMinutes() + val[0].getHours() * 60
      this.formData.workEndMinute1 =
        val[1].getMinutes() + val[1].getHours() * 60
    },
    /**
     * 新增工作模式
     */
    addWorkHistoryList() {
      if (this.formData.monday) {
        this.formData.monday = 1
      }
      if (this.formData.friday) {
        this.formData.friday = 1
      }
      if (this.formData.saturday) {
        this.formData.saturday = 1
      }
      if (this.formData.sunday) {
        this.formData.sunday = 1
      }
      if (this.formData.thursday) {
        this.formData.thursday = 1
      }
      if (this.formData.wednesday) {
        this.formData.wednesday = 1
      }
      if (this.formData.tuesday) {
        this.formData.tuesday = 1
      }

      this.$refs.form.validate(async flag => {
        if (!flag) return
        if (this.formData.id) {
          await updateWorkHistory(this.formData)
          this.$message.success('编辑成功')
        } else {
          await addWorkHistory(this.formData)
          this.$message.success('添加成功')
        }

        this.workHistory()
        this.closeDialog()
      })
    },
    /**
     * 关闭弹窗
     */
    closeDialog() {
      this.isDialog = false
      this.$refs.form.resetFields()
    },
    /**
 * 打开弹窗
 */
    openDialog() {
      this.isDialog = true
      this.formData.id = ''
    },
    /**
     * 删除
     */
    async deleteWorkHistoryList(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(async () => {
          await deleteWorkHistory(id)
          this.$message.success('删除成功')
          if (this.list.length === 1 && this.pagesize > 1) {
            this.page--
          }
          this.workHistory()
        })
        .catch(() => {})
    },
    /**
     * 渲染列表
     */
    async workHistory() {
      const res = await workHistoryList({
        page: this.page,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.list = res.data.items
      this.total = +res.data.counts
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.page = 1
      this.workHistory()
    },
    handleCurrentChange(val) {
      this.page = val
      this.workHistory()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.pag{
  text-align: center;
}
.el-ipt {
  width: 150px;

  margin-left: 20px;
  margin-right: 10px;
}
.i-ipt {
  color: #000;
  font-style: normal;
  margin-right: 10px;
}
.dialog-footer {
  text-align: center;
}
.one {
  font-size: 13px;
}
.two {
  font-size: 13px;
  color: #e15536;
}
.el-tab {
  margin-top: 20px;
  margin-bottom: 20px;
}
.top {
  margin-top: 20px;
}
.btn {
  background-color: #e15536;
  color: #fff;
}
/deep/ .table-card-box {
  .el-card__body {
    padding-bottom: 0px;
  }
}
/deep/ .pagination {
  padding-bottom: 30px;
}
.tab-box {
  color: #20232a;
  /deep/ .el-tabs__item.is-active {
    color: #20232a;
    font-size: 16px;
    font-weight: bold;
  }
  /deep/ .el-form-item__content {
    height: 40px;
  }
  /deep/ .el-tabs__item {
    font-size: 14px;
  }
  /deep/ .el-tabs__item:hover {
    color: #20232a;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
  }
  /deep/ .el-tabs__active-bar {
    background-color: #e15536;
  }
  /deep/ .tips {
    margin-left: 130px;
    margin-bottom: 14px;
    color: #bac0cd;
  }
  .el-checkbox {
    margin-right: 12px;
  }
  /deep/ .workDay {
    .el-form-item__error {
      position: relative;
    }
  }
}
/deep/ .el-dialog__body {
  padding-top: 30px !important;
  padding-left: 40px;
  padding-right: 40px;
  .el-tabs__header {
    padding-bottom: 25px;
  }
}
/deep/ .el-tabs__nav,
.is-top {
  transform: translateX(30px) !important;
}
</style>
